<?php
/**
 * Created by PhpStorm.
 * User: king
 * Date: 2017/1/29
 * Time: 下午3:00
 */

namespace App\Api;

use Illuminate\Routing\Controller;
use Dingo\Api\Routing\Helpers;
use Illuminate\Http\Request;
use \Curl\Curl;

class BaseController extends Controller {
    use Helpers;

    protected $curl;

    public function __construct() {
        $this->curl = $this->getCurlInstance();
    }

    /**
     * 随机产生六位数
     *
     * @param int $len
     * @param string $format
     * @return string
     */
    protected function __randStr($len = 6, $format = 'ALL') {
        switch ($format) {
            case 'ALL':
                $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-@#~';
                break;
            case 'CHAR':
                $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-@#~';
                break;
            case 'NUMBER':
                $chars = '0123456789';
                break;
            default :
                $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-@#~';
                break;
        }

        mt_srand((double)microtime() * 1000000 * getmypid());
        $password = "";

        while ( $l = strlen($password) < $len ) {
            if ($l == 1 && $password == '0') $password = '';
            $password .= substr($chars, (mt_rand() % strlen($chars)), 1);
        }

        return $password;
    }

    /**
     * 获取curl实例
     * @return Curl object
     */
    protected function getCurlInstance() {
        return new Curl();
    }
}
